<template>
    <div class="content" ref="content">
      <div class="table-default" v-if="tableData">
        <el-button type="primary" class="backBtn" @click="backHandler">返回</el-button>
        <!-- <el-button type="warning" class="backBtn" @click="approveHandler(route.query.id)" v-if="route.query.state != '已审核'" v-loading.fullscreen.lock="fullscreenLoading"><Icon name="wrench-screwdriver" :size="16" />审核</el-button> -->
        <!-- <el-button type="success" @click="editClick" v-if="route.query.state != '已审核'">保存修改</el-button> -->
        <div id="base" ref="base">
          <el-descriptions :column="3" border title="基本信息">
            <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="销售退货单号">{{ tableData.Salesreturn.returnnumber }}</el-descriptions-item>
            <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="备注">{{ tableData.Salesreturn.remark }}</el-descriptions-item>
            <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="回写状态">{{ tableData.Salesreturn.writestate }}</el-descriptions-item>
          </el-descriptions>
        </div>
  
        <div id="details" ref="details">
          <el-tabs v-model="activeName" class="demo-tabs">
            <el-tab-pane label="销售退货-明细" name="details">
              <el-table :data="tableData.detailscheck_all" border>
                <el-table-column prop="fsaleorgid_name" label="销售组织名称" width="130" />
                <el-table-column prop="fcustomerid_number" label="客户编码" width="130" />
                <el-table-column prop="fcustomerid_name" label="客户名称" width="130" />
                <el-table-column prop="fmaterialid" label="品号" width="130" />
                <el-table-column prop="fmaterialname" label="品名" width="250" />
                <el-table-column prop="fmaterialmodel" label="规格" width="200" />
                <el-table-column prop="fmodel" label="型号" width="200" />
                <el-table-column prop="barcode" label="箱号" width="200" />
                <el-table-column prop="quantity" label="销售退货数量" width="180" v-if="route.query.state != '已审核'">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.quantity" :min="1" />
                  </template>
                </el-table-column>
                <el-table-column prop="quantity" label="销售退货数量" width="180" v-if="route.query.state == '已审核'">
                  {{ Number(scope.row.quantity) }}
                </el-table-column>
                <el-table-column prop="funit_name" label="单位" width="80" />
                <el-table-column prop="fnumber_stash" label="仓库名称" width="130" />
                <el-table-column prop="fname_stash" label="仓库编码" width="130" />
  
                <el-table-column prop="repositoryname" label="库位名称" width="130" />
                <el-table-column prop="warehousecode" label="库位代码" width="130" />

              </el-table>
              <Paginate />
            </el-tab-pane>
  
            <el-tab-pane label="销售退货-汇总" name="summary" v-if="summaryData">
              <el-table :data="summaryData.detailscheck_all" border>
                <el-table-column prop="fsaleorgid_name" label="销售组织名称" width="130" />
                <el-table-column prop="fcustomerid_number" label="客户编码" width="130" />
                <el-table-column prop="fcustomerid_name" label="客户名称" width="130" />
                <el-table-column prop="fmaterialid" label="品号" width="130" />
                <el-table-column prop="fmaterialname" label="品名" width="250" />
                <el-table-column prop="fmaterialmodel" label="规格" width="250" />
                <el-table-column prop="fmodel" label="型号" width="250" />
                <el-table-column prop="quantity" label="销售退货数量" width="180" v-if="route.query.state != '已审核'">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.quantity" :min="1" />
                  </template>
                </el-table-column>
                <el-table-column prop="quantity" label="销售退货数量" width="180" v-if="route.query.state == '已审核'">
                  {{ Number(scope.row.quantity) }}
                </el-table-column>
                <el-table-column prop="funit_name" label="单位" width="80" />
                <el-table-column prop="fnumber_stash" label="仓库名称" width="130" />
                <el-table-column prop="fname_stash" label="仓库编码" width="130" />
  
                <el-table-column prop="repositoryname" label="库位名称" width="130" />
                <el-table-column prop="warehousecode" label="库位代码" width="130" />

              </el-table>
              <Paginate />
            </el-tab-pane>
          </el-tabs>
        </div>
        <!-- </el-scrollbar> -->
      </div>
    </div>
  </template>
  <script lang="ts" setup>
  import { computed, onMounted, ref } from 'vue'
  import { useGetList } from '/admin/composables/curd/useGetList'
  import { useDestroy } from '/admin/composables/curd/useDestroy'
  import { useOpen } from '/admin/composables/curd/useOpen'
  import { useRouter, useRoute } from 'vue-router'
  import detailTableVue from './detaiTable.vue'
  import http from '/admin/support/http'
  import { ElMessage } from 'element-plus'
  
  const router = useRouter()
  const route = useRoute()
  
  const api = `salesmanagement/salesreturn/${route.query.id}`
  const { data, query, search, reset, loading } = useGetList(api)
  const { destroy, deleted } = useDestroy()
  const { open, close, title, visible, id } = useOpen()
  
  const tableData = computed(() => data.value?.data.data)
  
  const tabName = ref('')
  
  console.log(tableData)
  // const rowInfo = JSON.parse(route.query.rowInfo)
  // console.log(rowInfo)
  
  const summaryData = ref([])
  
  const state = ref(route.query.state)
  
  const getDetailsAll = () => {
    // detailsAll=true
    const api2 = `salesmanagement/salesreturn/${route.query.id}?all=27`
    http.get(api2).then(r => {
      const res = r.data.data
      console.log('汇总数据：', res)
      summaryData.value = res.data
    })
  }
  
  getDetailsAll()

  const fullscreenLoading = ref(false)

const approveHandler = id => {
  fullscreenLoading.value = true
  http
    .put(`/salesmanagement/interfaceaudit/${id}`)
    .then(r => {
      fullscreenLoading.value = false
      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}
  
  const writestateFormatter = ref({
    0: '回写失败',
    1: '回写成功',
  })
  
  const searchbox = ref(null)
  const showRef = ref(null)
  const flag = ref(false)
  
  console.log('路由参数接收', route.query.id)
  
  const backHandler = () => {
    router.push('/Salesmanagement/salesreturn')
  }
  const isMore = ref<boolean>(false)
  
  const activeName = ref('details')
  const base = ref(null)
  const details = ref(null)
  const content = ref()
  const currentId = route.query.id
  
  // 修改
  const editClick = () => {
    let detailObj = tableData.value.map(item => {
      return {
        // id: item.id,
        quantity: item.quantity,
      }
    })
    console.log('详情修改数据：', detailObj)
  
    let AllObj = summaryData.value.map(item => {
      return {
        giveaway: item.giveaway,
        provisional_id: item.provisional_id,
        purchaseorder_id: item.purchaseorder_id,
        purchaseorderschedule_id: item.purchaseorderschedule_id,
        supplier_id: item.supplier_id,
        purchasewarehousing_id: item.purchasewarehousing_id,
        inspectionlisttable_id: item.inspectionlisttable_id,
      }
    })
  
    console.log('汇总修改数据：', AllObj)
  
    // updateDetailsAll(AllObj)
    updateDetail(detailObj)
  }
  
  const updateDetail = data => {
    http.put(`/intelligent/otheroutbound/10/${currentId}`, data).then(r => {
      console.log(r.data.data)
      let res = r.data.data
      if ((res.code = '1000')) {
        ElMessage({
          message: res.message,
          type: 'success',
        })
      } else {
        ElMessage({
          message: res.message,
          type: 'error',
        })
      }
    })
  }
  const updateDetailsAll = data => {
    http.put(`/purchasing/summaryupdatedata/${currentId}`, data).then(r => {
      console.log(r.data.data)
      let res = r.data.data
  
      if ((res.code = '1000')) {
        ElMessage({
          message: res.message,
          type: 'success',
        })
      } else {
        ElMessage({
          message: res.message,
          type: 'error',
        })
      }
    })
  }
  
  onMounted(() => {
    search()
    deleted(reset)
  })
  </script>
  
  <style lang="css">
  /* .content{
    border: 2px solid red;
   overflow:hidden;
  } */
  .backBtn {
    margin: 20px 10px;
  }
  
  h2 {
    font-weight: bold;
  }
  #base {
    margin-bottom: 30px;
    /* border: 2px solid green; */
  }
  #details {
    /* border: 2px solid pink; */
  }
  
  .tabs {
    padding: 10px 0 0;
    /* border: 2px solid #000; */
    z-index: 999;
    background-color: #fff;
    position: sticky;
    top: -15px;
  }
  
  .tagRow {
    display: inline-block;
    /* border: 1px solid #000; */
    margin: 20px 10px;
  }
  
  .tagRow ul {
    display: flex;
    border: 1px solid #eee;
  }
  .tagRow ul li {
    margin: 0 5px;
    /* border: 1px solid #eee; */
    padding: 5px 10px;
    /* border-radius: 5px; */
  }
  .active {
    background-color: #409eff;
    color: #fff;
  }
  .labelClassNAme {
    width: 130px;
  }
  .contentClassName {
    width: 170px;
  }
  </style>
  